package com.tp;

import java.util.ArrayList;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.ListActivity;
import android.content.ContextWrapper;
import android.content.Intent;
import android.net.ParseException;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class ListTrip extends ListActivity {
	static JSONArray jArr;
	String items[];
	static int flag = 0;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// setContentView(R.layout.activity_list_trip);
		if (flag == 0) {
			ArrayList<String> result = getTrip();
			items = new String[result.size()];
			items = result.toArray(items);
			setListAdapter(new ArrayAdapter<String>(this,
					android.R.layout.simple_list_item_1, items));
		} else if (flag == 1) {
			setListAdapter(new ArrayAdapter<String>(this,
					android.R.layout.simple_list_item_1, SearchTrip.trip));
			// flag =0;
		}

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_list_trip, menu);
		return true;
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		switch (keyCode) {
		case KeyEvent.KEYCODE_BACK:
			flag = 0;
			startActivity(new Intent(this, Main.class));
			break;

		}
		return false;
	}

	private ArrayList<String> getTrip() {
		JSONArray jArray;
		JSONObject json_data = null;
		int i;
		ArrayList<String> r = new ArrayList<String>();
		ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();
		postParameters
				.add(new BasicNameValuePair("id_User", "" + Constant.user));

		String result = null;
		try {
			result = CustomHttpClient.executeHttpPost(Constant.host
					+ "/TripController/getTrip", postParameters);

			if (result != "") {

				jArray = new JSONArray(result);
				jArr = jArray;
				for (i = 0; i < jArray.length(); i++) {
					json_data = jArray.getJSONObject(i);
					r.add(json_data.getString("name") + ", Ngày "
							+ json_data.getString("dateStart"));
				}
				// return r;
			}
		} catch (JSONException e1) {
			Toast.makeText(((ContextWrapper) this).getBaseContext(),
					e1.toString(), Toast.LENGTH_SHORT).show();
		} catch (ParseException e1) {
			Toast.makeText(((ContextWrapper) this).getBaseContext(),
					e1.toString(), Toast.LENGTH_SHORT).show();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return r;

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.app.ListActivity#onListItemClick(android.widget.ListView,
	 * android.view.View, int, long)
	 */
	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		// TODO Auto-generated method stub
		Trip.flag = 1;
		String value = getListView().getItemAtPosition(position).toString();
		if (flag == 0) {
			for (int i = 0; i < jArr.length(); i++) {
				JSONObject json_data;
				try {
					json_data = jArr.getJSONObject(i);
					if ((json_data.getString("name") + ", Ngày " + json_data
							.getString("dateStart")).equals(value)) {
						Trip.trip_name = json_data.getString("name");
						Trip.startDate = json_data.getString("dateStart");
						Trip.id_Trip = json_data.getInt("id_Trip");
						Trip.duration = json_data.getInt("duration");
						Log.v("AA_IDTRIP", Trip.id_Trip + "");

						startActivity(new Intent(this, Trip.class));
					}
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

			}
		} else if (flag == 1) {
			flag = 0;
			Trip.setAdap = 1;
			for (int i = 0; i < SearchTrip.jArr.length(); i++) {
				JSONObject json_data;
				try {
					json_data = SearchTrip.jArr.getJSONObject(i);
					if ((json_data.getString("name") + ", Ngày "
							+ json_data.getString("dateStart")
							+ ", Ngày thứ " + json_data.getInt("day"))
							.equals(value)) {
						Trip.trip_name = json_data.getString("name");
						Trip.startDate = json_data.getString("dateStart");
						Trip.id_Trip = json_data.getInt("id_Trip");
						Trip.duration = json_data.getInt("duration");
						Trip.selectDay = json_data.getInt("day");
						// Trip.day_ = json_data.getInt("day");
						Log.v("AA_IDTRIP", Trip.id_Trip + "");
						if (json_data.getInt("id_User") == Constant.user)
							Trip.allow = 1;
						else
							Trip.allow = 0;

						startActivity(new Intent(this, Trip.class));
					}
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

			}
		}
	}

}
